// 导出页面为PDF格式
import html2Canvas from "html2canvas";
import JsPDF from "jspdf";
const htmlToPdf = {
  getPdf(dom,options) {
    let title = options.title;
    html2Canvas(dom, {
      background: "#ffffff", // 一定要添加背景颜色，否则出来的图片，背景全部都是透明的
      scale: 2, // 处理模糊问题
      dpi: 300, // 处理模糊问题
      allowTaint: true,
      useCORS: true /*使用跨域*/
    }).then(function(canvas) {

      //创建新pdf
      let pdf = new JsPDF(options.direction?options.direction:'p', 'pt', 'a4')
      const pdfWidth = pdf.internal.pageSize.getWidth();
      const pdfHeight = pdf.internal.pageSize.getHeight();

      //未生成pdf的html页面高度
      let leftHeight = canvas.height

      //image宽高
      let paperWidth = Math.floor(options.paper?options.paper.width * 2.83:595.28);
      let paperHeight = Math.floor(options.paper?options.paper.height * 2.83:841.89);
      //一页pdf显示html页面生成的canvas高度;
      let paperHeightRef = Math.floor(canvas.width / paperWidth * paperHeight)

      //pdf页面偏移
      let position = 0

      let pageData = canvas.toDataURL('image/jpeg', 1.0)

      let index = 1,
          canvas1 = document.createElement('canvas'),
          height
      pdf.setDisplayMode('fullwidth', 'continuous', 'FullScreen');
      function createImpl(canvas) {
        if (leftHeight > 0) {
          index++;
          let checkCount = 0
          if (leftHeight > paperHeightRef) {
            let i
            for (i = position + paperHeightRef; i >= position; i--) {
              let isWrite = true
              for (let j = 0; j < canvas.width; j++) {
                let ctx = canvas.getContext('2d', { willReadFrequently:true });

                let c = ctx.getImageData(j, i, 1, 1).data

                if (c[0] !== 0xff || c[1] !== 0xff || c[2] !== 0xff) {
                  isWrite = false
                  break
                }
              }
              if (isWrite) {
                checkCount++
                if (checkCount >= 10) {
                  break
                }
              } else {
                checkCount = 0
              }
            }
            height = Math.round(i - position) || Math.min(leftHeight, paperHeightRef);
            if (height <= 0) {
              height = paperHeightRef;
            }
          } else {
            height = leftHeight;
          }

          canvas1.width = canvas.width;
          canvas1.height = height;

          // console.log(index, 'height:', height, 'pos', position);

          let ctx = canvas1.getContext('2d', { willReadFrequently: true });
          ctx.drawImage(canvas, 0, position, canvas.width, height, 0, 0, canvas.width, height);

          // let pageHeight = Math.round(paperWidth / canvas.width * height);
          // pdf.setPageSize(null, pageHeight)
          if (position !== 0) {
            pdf.addPage();
          }
          pdf.addImage(canvas1.toDataURL('image/jpeg', 1.0), 'JPEG', 0, 0, paperWidth, paperWidth / canvas1.width * height);
          leftHeight -= height;
          position += height;
          if (leftHeight > 0) {
            setTimeout(createImpl, 500, canvas);
          } else {
            pdf.save(title + '.pdf');
          }
        }
      }

      //当内容未超过pdf一页显示的范围，无需分页
      if (leftHeight <= paperHeightRef) {
        pdf.addImage(pageData, 'JPEG', 0, 0, paperWidth, paperWidth / canvas.width * leftHeight);
        pdf.save(title + '.pdf')
      } else {
        try {
          pdf.deletePage(0);
          setTimeout(createImpl, 500, canvas);
        } catch (err) {
          console.log(err);
        }
      }
    })
  }
}
export default htmlToPdf
